import axios from "axios";
import { message } from "antd";
// import { getToken } from "@/utils/catch";
import Router from "next/router";
const service = axios.create({
  // baseURL: "http://localhost:4000/api"
  // baseURL: "http://49.235.175.45/blogapi/api/",
  baseURL:
    process.env.NODE_ENV === "production"
      ? "http://49.235.175.45/blogapi/api/"
      : "http://localhost:4000/api",
});

// service.interceptors.request.use(
//   config => {
//     if (typeof window !== 'undefined') {
//       const token = getToken();
//       if (token) {
//         config.headers.Authorization = token;
//       }
//     }
//     return config;
//   },
//   err => {
//     throw new Error("发出请求的过程中出现错误");
//   }
// );

service.interceptors.response.use(
  (data) => {
    if (data.status && data.status == 200 && data.data.status == "error") {
      typeof window !== "undefined" &&
        message.error({ message: data.data.msg });
      return;
    }

    const res = data.data;

    if (!res.success) {
      message.error(res.msg);
      return;
    }

    return res.data;
  },
  (err) => {
    if (err && err.response && err.response.status) {
      const status = err.response.status;

      switch (status) {
        case 504:
        case 404:
          typeof window !== "undefined" && message.error("服务器异常");
          break;

        case 403:
          typeof window !== "undefined" && message.warn("访客无权进行该操作");
          break;

        case 401:
          typeof window !== "undefined" && message.info("请重新登录");
          Router.push("/login");
          break;

        default:
          typeof window !== "undefined" &&
            message.error(
              (err.response && err.response.data && err.response.data.msg) ||
                "未知错误!"
            );
      }
    }

    return Promise.reject(err);
  }
);

export default service;
